﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using log4net;

namespace CompanyManagement.Controllers
{
    public class ProjectController : Controller
    {
        //
        // GET: /Project/
        ProjectDLL dllProject = new ProjectDLL();
        UserDLL dllUser = new UserDLL();
        private static readonly ILog Log = LogManager.GetLogger(typeof(ProjectController));

        #region Page View

        public ActionResult Index()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            Utility.setCookiValue();
            List<ProjectMetaData> productlist = new List<ProjectMetaData>();
            try
            {
                if (Utility.getRoleID() == 1)
                {
                    productlist = dllProject.getProjectListByUserID(0);
                }
                else
                {
                    productlist = dllProject.getProjectListByUserID(Utility.getUserID());
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(productlist);
        }

        public ActionResult Create()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            return View();
        }

        public ActionResult Edit(int id)
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }
            ProjectMetaData ProjectMetaData = new ProjectMetaData();
            try
            {
                ProjectMetaData = dllProject.GetProjectByID(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return View(ProjectMetaData);
        }

        #endregion

        #region DML Operation

        [HttpPost]
        public ActionResult Create(ProjectMetaData projectmetadata)
        {
            ProjectMetaData ProjectMetaData = new ProjectMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    if (projectmetadata.EndDate > projectmetadata.StartDate)
                    {
                        projectmetadata.CreatedBy = Utility.getUserID();
                        int projectid = dllProject.InsertProject(projectmetadata);

                        if (!string.IsNullOrEmpty(projectmetadata.CheckUser))
                        {
                            foreach (var item in projectmetadata.CheckUser.Split(','))
                            {
                                string assignid = Convert.ToString(item);
                                if (!string.IsNullOrEmpty(assignid) && assignid != ",")
                                {
                                    int id1 = Convert.ToInt32(assignid);

                                    ProjectAssignMetaData assigndata = new ProjectAssignMetaData();
                                    assigndata.AssignID = id1;
                                    assigndata.ProjectID = projectid;
                                    new ProjectAssignDLL().InsertAssign(assigndata);

                                }
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("DateInvalid", "EndDate must be greater then Start Date.");
                        return View(projectmetadata);
                    }
                }
                else
                {
                    return View(projectmetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(projectmetadata);
            }
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Edit(ProjectMetaData projectmetadata)
        {
            ProjectMetaData ProjectMetaData = new ProjectMetaData();
            try
            {
                if (ModelState.IsValid)
                {
                    if (projectmetadata.EndDate > projectmetadata.StartDate)
                    {
                        projectmetadata.ModifyBy = Utility.getUserID();
                        dllProject.UpdateProject(projectmetadata);

                        var existinglist = new ProjectAssignDLL().getProjectAssignListByProjectID(projectmetadata.PorjectID);

                        if (existinglist != null)
                        {
                            foreach (var data in existinglist)
                            {
                                new ProjectAssignDLL().DeleteAssign(data.ID);
                            }
                        }

                        if (!string.IsNullOrEmpty(projectmetadata.CheckUser))
                        {
                            foreach (var item in projectmetadata.CheckUser.Split(','))
                            {
                                string assignid = Convert.ToString(item);
                                if (!string.IsNullOrEmpty(assignid) && assignid != ",")
                                {
                                    int id1 = Convert.ToInt32(assignid);

                                    ProjectAssignMetaData assigndata = new ProjectAssignMetaData();
                                    assigndata.AssignID = id1;
                                    assigndata.ProjectID = projectmetadata.PorjectID;
                                    new ProjectAssignDLL().InsertAssign(assigndata);

                                }
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("DateInvalid", "EndDate must be greater then Start Date.");
                        return View(projectmetadata);
                    }
                }
                else
                {
                    return View(projectmetadata);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                return View(projectmetadata);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id)
        {
            try
            {
                dllProject.DeleteProject(id);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
            return RedirectToAction("Index");
        }
        #endregion

        #region Project Assign

        public ActionResult ProjectAssign()
        {
            List<UserMetaData> userList = new List<UserMetaData>();
            userList = dllUser.GetUserList().Where(u => u.RoleID != 1).ToList();
            return PartialView(userList);
        }

        public JsonResult getProjectDetail(int projectId)
        {
            List<UserMetaData> projectDetail = dllUser.getUserListByProjectID(projectId);
            var collection = projectDetail.Select(x => new
            {
                UserID = x.UserID,
                ProjectID = x.RoleID,
                TID = x.TID,
                UserName = x.UserName
            });
            return Json(collection, JsonRequestBehavior.AllowGet);
        }

        #endregion

    }
}
